<template>
	<view class="content">

		<view class="text">
			选择{{deviceName}}服务
		</view>
		<view class="bluetooth-list">
			<view class="items text" @click="getBLEDeviceCharacteristics(item)" v-for="(item,index) in seviceList"
				:key="index">
				{{item['uuid']}}
			</view>
		</view>
		<view class="text" v-show="characteristics.length > 0">
			特征值:
		</view>
		<!-- <view v-show="characteristics.length > 0" class="bluetooth-list">
			<view class="items class=" text"" v-for="(item,index) in characteristics" :key="index">
				{{item['uuid']}} {{item['typeName']}}
			</view>
		</view> -->
		<view v-show="characteristics.length > 0">
			<button size="mini" @click="toControl" hover-start-time=20>跳转控制页面</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				seviceList: [],
				characteristics: [],
				deviceName: '无绑定',
				deviceId: '',
				serviceId: '',
				writeuuid: '',
				notifyuuid: ''
			}
		},
		onShow() {
			this.deviceId = uni.getStorageSync('deviceId')
			this.deviceName = uni.getStorageSync('deviceName')
			this.getBLEDeviceServices();
		},
		methods: {
			getBLEDeviceServices() {
				console.log(1);
				uni.showLoading({
					title: '获取服务加载中'
				});
				const thit = this;
				setTimeout(() => {
					uni.getBLEDeviceServices({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
						deviceId: thit.deviceId,
						success: (res) => {
							uni.hideLoading()
							thit.seviceList = res['services'];
						},
						fail: (e) => {
							console.log(e)
							uni.hideLoading()
						}
					})
				}, 2000)
			},
			getBLEDeviceCharacteristics(params) {
				console.log("获取特征值", params);
				uni.showLoading({
					title: '获取特征值加载中'
				});
				this.serviceId = params['uuid']
				const that = this;
				setTimeout(() => {
					uni.getBLEDeviceCharacteristics({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
						deviceId: that.deviceId,
						// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
						serviceId: params['uuid'],
						success: (res) => {
							uni.hideLoading()
							console.log(res, '特征值')
							res.characteristics.forEach((item) => {
								if (item['properties']['write']) { // 标注写入特征值
									that.characteristics.push({
										uuid: item.uuid,
										type: 'write',
										typeName: "下发"
									})
								}
								if (item['properties']['notify']) { // 标注写入特征值
									that.characteristics.push({
										uuid: item.uuid,
										type: 'notify',
										typeName: "上报"
									})
								}
							})
							if (that.characteristics.length === 0) {
								uni.showToast({
									icon: 'none',
									title: '该服务特征值不符合'
								});
							}

						},
						fail: (res) => {
							uni.hideLoading()
							console.log(res)
						}
					})
				}, 1000)
			},
			toControl() {
				uni.setStorageSync('characteristics', this.characteristics)
				uni.setStorageSync('serviceId', this.serviceId)
				uni.navigateTo({
					url: `/pages/control/index`
				})
			}
		}
	}
</script>

<style>
	.bluetooth-list {
		width: 90%;
		margin: 0 auto;
		/* background: antiquewhite; */
	}

	.content {
		padding: 0 50px;
		display: flex;
		flex-direction: column;
		/* align-items: center; */
		/* justify-content: center; */
		background: #0f3854;
		background: radial-gradient(ellipse at center, #0a2e38 0%, #000000 70%);
		width: 100%;
		height: 100%;
		/* z-index: 1; */
		position: fixed;
	}

	.text {
		font-family: "Share Tech Mono", monospace;
		color: #ffffff;
		/* text-align: center; */
		color: #daf6ff;
		text-shadow: 0 0 20px #0aafe6, 0 0 20px rgba(10, 175, 230, 0);
	}

	.items {
		height: 30px;
		margin-top: 20px;
	}
</style>
